/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: MenuNode
 * Author:   zhouheng
 * Date:     2018/7/4 16:17
 * Description: 菜单节点
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.hengby.hengcrud.core.node;

import com.hengby.hengcrud.core.constants.IsMenu;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈菜单节点〉
 *
 * @author zhouheng
 * @create 2018/7/4
 * @since 1.0.0
 */
@Getter
@Setter
public class MenuNode implements Comparable {

    // 节点id
    private Integer id;

    // 节点名称
    private String name;

    // 父节点Id
    private Integer pId;

    // 按钮级别
    private Integer levels;

    // 是否是菜单
    private Integer ismenu;

    // 节点排序
    private Integer num;

    // 节点url
    private String url;

    // 节点图标
    private String icon;

    // 子节点集合
    private List<MenuNode> list;

    private List<MenuNode> linkedList = new ArrayList<MenuNode>();

    @Override
    public int compareTo(Object o) {
        return 0;
    }

    /**
     * 构建节点树
     * @param menus
     * @return
     */
    public static List<MenuNode> buildTitle(List<MenuNode> menus) {

        // 获取所有菜单资源
        List<MenuNode> menuList = clearBtn(menus);

        new MenuNode().buildNodeTree(menuList);

        List<MenuNode> menuNodes = clearLevelTwo(menuList);

        return menuNodes;

    }

    // 清除levels = 2
    private static List<MenuNode> clearLevelTwo(List<MenuNode> menuList) {
        List<MenuNode> list = new ArrayList<>();

        for (MenuNode menuNode : menuList){
            if (menuNode.getLevels() == 1){
                list.add(menuNode);
            }
        }

        return list;
    }

    // 构建菜单树
    private void buildNodeTree(List<MenuNode> menuList) {
        for (MenuNode menuNode : menuList){
            List<MenuNode> linkedList =  menuNode.findChildNodes(menuNode.getId(),menuList);
            if (linkedList.size() > 0) {
                menuNode.setList(linkedList);
            }
        }
    }

    private List<MenuNode> findChildNodes(Integer pId,List<MenuNode> menuList) {
        for (MenuNode menuNode : menuList){
            if (menuNode.getPId() != null &&  menuNode.getPId().equals(pId)){
                recursionFn(menuNode, pId, menuList);
            }
        }

        return linkedList;
    }

    private void recursionFn(MenuNode menuNode, Integer pId, List<MenuNode> menuList) {
        // 获取子节点
        List<MenuNode> childList = getChildList(menuNode,menuList);
        if (childList != null && childList.size() > 0){

            if (pId.equals(menuNode.getPId())){
                linkedList.add(menuNode);
            }

            Iterator<MenuNode> it = childList.iterator();
            while (it.hasNext()) {
                MenuNode n = (MenuNode) it.next();
                recursionFn(n, n.getPId(),menuList);
            }
        }else {
            if (pId.equals(menuNode.getPId())){
                linkedList.add(menuNode);
            }
        }
    }

    private List<MenuNode> getChildList(MenuNode node, List<MenuNode> menuList) {
        List<MenuNode> nodeList = new ArrayList<MenuNode>();
        Iterator<MenuNode> it = menuList.iterator();
        while (it.hasNext()) {
            MenuNode n = (MenuNode) it.next();
            if (n.getPId().equals(node.getId())) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }


    // 清除按钮资源，获取菜单资源
    private static List<MenuNode> clearBtn(List<MenuNode> menus) {
        ArrayList<MenuNode> menuList = new ArrayList<MenuNode>();

        for (MenuNode menuNode : menus){
            if (menuNode.getIsmenu() == IsMenu.YES.getCode()){
                menuList.add(menuNode);
            }
        }

        return menuList;
    }
}